/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.killHanlder;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 *
 * @author Administrator
 */
public class KillHandler implements SignalHandler {

    public static Logger logger = org.apache.log4j.Logger.getLogger(KillHandler.class.getName());

    @Override
    public void handle(Signal signal) {
        //写入文件
        if (signal.getName().equals("TERM")) {
            try {
                String path = ClassLoader.getSystemClassLoader().getResource("cconfiguration.properties").getPath();
                logger.info(ClassLoader.getSystemClassLoader().getResource("cconfiguration.properties").getPath());
                Properties prop = new Properties();
                prop.load(new FileInputStream(path));

                FileOutputStream oFile = new FileOutputStream(path, true);//true表示追加打开
                prop.setProperty("wxFansCntQueryCount", String.valueOf(RuntimeEnv.wxFansCntQueryCount.get()));
                prop.setProperty("wxReadCntCount", String.valueOf(RuntimeEnv.wxReadCntCount.get()));
                prop.setProperty("wxRealIdQueryCount", String.valueOf(RuntimeEnv.wxRealIdQueryCount.get()));
                prop.setProperty("wxPictureUrlQueryCount", String.valueOf(RuntimeEnv.wxPictureUrlQueryCount.get()));
                prop.store(oFile, "update success");
                oFile.close();
            } catch (IOException ex) {
                logger.error(ex);
            }
        }
    }

    public void registerSignal(String signalName) {
        Signal signal = new Signal(signalName);
        Signal.handle(signal, this);
    }
//    public static void main(String[] args) throws FileNotFoundException {
//        try {
//            String path = ClassLoader.getSystemClassLoader().getResource("cconfiguration.properties").getPath();
//            logger.info(ClassLoader.getSystemClassLoader().getResource("cconfiguration.properties").getPath());
//            Properties prop = new Properties();
//            prop.load(new FileInputStream(path));
//
//            FileOutputStream oFile = new FileOutputStream(path, true);//true表示追加打开
//            prop.setProperty("wxFansCntQueryCount", String.valueOf(RuntimeEnv.wxFansCntQueryCount.get()));
//            prop.setProperty("wxReadCntCount", String.valueOf(RuntimeEnv.wxReadCntCount.get()));
//            prop.setProperty("wxRealIdQueryCount", String.valueOf(RuntimeEnv.wxRealIdQueryCount.get()));
//            prop.setProperty("wxPictureUrlQueryCount", String.valueOf(RuntimeEnv.wxPictureUrlQueryCount.get()));
//            prop.store(oFile, "update success");
//            oFile.close();
//        } catch (IOException ex) {
//            logger.error(ex);
//        }
//    }
}
